﻿using System;
using System.Security.Principal;
using NPMonitor.Attributes;
using NPMonitor.DataModel;
using NPMonitor.Utility;
using OpenQA.Selenium.Remote;

namespace PerformanceTest
{
    public class RunJob : WebJobBase
    {
        protected override void ExecuteJob(HttpWatch.Plugin plugin)
        {
            WebDriverHelper.WebDriver = WebDriver;
            WebDriverHelper.DefaultTimeoutSecs = DefaultTimeoutSecs;

            WindowsIdentity tempWindowsIdentity = WindowsIdentity.GetCurrent();
            tempWindowsIdentity.Impersonate();

            Console.WriteLine("Starting Step 1 ");
            Step1(plugin);
            Console.WriteLine("Step 1 - " + " Finished");

            Console.WriteLine("Starting Step 2 ");
            Step2(plugin);
            Console.WriteLine("Step 2 - " + " Finished");

            Console.WriteLine("Starting Step 3 ");
            Step3(plugin);
            Console.WriteLine("Step 3 - " + " Finished");

            Console.WriteLine("Starting Step 4 ");
            Step4(plugin);
            Console.WriteLine("Step 4 - " + " Finished");

            return;
        }

        [Scenario("PerformanceTest_1", "PerformanceTest_Step_1", "Step 1", null)]
        public void Step1(HttpWatch.Plugin plugin)
        {
            try
            {
                plugin.NextPageComment = "PerformanceTest_1";
                WebDriver.Navigate().GoToUrl("https://dsm.onbmc.com/");
                Control.WaitEx(plugin, -1, true, true, 0);
            }
            catch (Exception ex)
            {
                ExecutionResult.ErrorMessage = ex.Message;
                if (ex.InnerException != null)
                {
                    ExecutionResult.ErrorMessage = ex.Message + " -----> " + ex.InnerException.Message;
                }
                ExecutionResult.ResultStatus = JobResultStatus.Fail;
            }
        }

        [Scenario("PerformanceTest_2", "PerformanceTest_Step_2", "Step 2", null)]
        public void Step2(HttpWatch.Plugin plugin)
        {
            try
            {
                //Click Application Menu
                //var navApplicationMenu = WebDriverHelper.GetById(WebDriver, "WIN_0_304316340");
                //navApplicationMenu.Click();

                //var divLeftMenu = WebDriverHelper.GetById(WebDriver, "WIN_0_80077");
                //((RemoteWebDriver)WebDriver).ExecuteScript("javascript:CallARGSHR_58OVC_58AppListEntryPointonbmc_45sEPFunc(false);");

                plugin.NextPageComment = "PerformanceTest_2";
                //Click on Show: Assigned To All My Groups. 
                var divSelect = WebDriverHelper.GetById(WebDriver, "WIN_2_303174300");
                var selectLink = WebDriverHelper.GetByTagName(divSelect, "a");
                selectLink.Click();

                var divSelectTable = WebDriverHelper.GetByClassName(WebDriver, "MenuOuter");
                var selectTable = WebDriverHelper.GetByTagName(divSelectTable, "table");
                var selectTr = WebDriverHelper.GetWebElementsByTagName(selectTable, "tr");
                selectTr[3].Click();

                Control.WaitEx(plugin, DefaultTimeoutSecs, true, true, 0);
            }
            catch (Exception ex)
            {
                ExecutionResult.ErrorMessage = ex.Message;
                if (ex.InnerException != null)
                {
                    ExecutionResult.ErrorMessage = ex.Message + " -----> " + ex.InnerException.Message;
                }
                ExecutionResult.ResultStatus = JobResultStatus.Fail;
            }
        }

        [Scenario("PerformanceTest_3", "PerformanceTest_Step_3", "Step 3", null)]
        public void Step3(HttpWatch.Plugin plugin)
        {
            try
            {
                plugin.NextPageComment = "PerformanceTest_3";
                //Double click on one of the incident tickets.
                var tableTickets = WebDriverHelper.GetById(WebDriver, "T301444200");
                var trsTickets = WebDriverHelper.GetWebElementsByTagName(tableTickets, "tr");
                var tdsTickets = WebDriverHelper.GetWebElementsByTagName(trsTickets[1], "td");
                WebDriverHelper.DoubleClick(WebDriver, tdsTickets[0]);

                var foundText = WebDriverHelper.GetById(WebDriver, "arid_WIN_3_1000000161");

                //Click Application Menu
                var navApplicationMenu = WebDriverHelper.GetById(WebDriver, "WIN_0_304316340");
                navApplicationMenu.Click();

                //Go to the Change Management Console 
                var divLeftMenu = WebDriverHelper.GetById(WebDriver, "WIN_0_80077");
                ((RemoteWebDriver)WebDriver).ExecuteScript("javascript:CallARGCHG_58CHC_58AppListEntryPointonbmc_45sEPFunc(false);");

                //Click on Show: Assigned To All My Groups. 
                var divSelect = WebDriverHelper.GetById(WebDriver, "WIN_4_303174300");
                var selectLink = WebDriverHelper.GetByTagName(divSelect, "a");
                selectLink.Click();

                var divSelectTable = WebDriverHelper.GetByClassName(WebDriver, "MenuOuter");
                var selectTable = WebDriverHelper.GetByTagName(divSelectTable, "table");
                var selectTr = WebDriverHelper.GetWebElementsByTagName(selectTable, "tr");
                selectTr[4].Click();

                //Double click on one of the incident tickets.
                var tableChanges = WebDriverHelper.GetById(WebDriver, "T301872400");
                var trsChanges = WebDriverHelper.GetWebElementsByTagName(tableChanges, "tr");
                var tdsChanges = WebDriverHelper.GetWebElementsByTagName(trsChanges[1], "td");
                WebDriverHelper.DoubleClick(WebDriver, tdsChanges[0]);

                var foundText1 = WebDriverHelper.GetById(WebDriver, "arid_WIN_5_1000000182");
            }
            catch (Exception ex)
            {
                ExecutionResult.ErrorMessage = ex.Message;
                if (ex.InnerException != null)
                {
                    ExecutionResult.ErrorMessage = ex.Message + " -----> " + ex.InnerException.Message;
                }
                ExecutionResult.ResultStatus = JobResultStatus.Fail;
            }
        }

        [Scenario("PerformanceTest_4", "PerformanceTest_Step_4", "Step 4", null)]
        public void Step4(HttpWatch.Plugin plugin)
        {
            try
            {
                plugin.NextPageComment = "PerformanceTest_4";
                //Click Application Menu
                var navApplicationMenu = WebDriverHelper.GetById(WebDriver, "WIN_0_304316340");
                navApplicationMenu.Click();

                //Go to the Change Management Console 
                var divLeftMenu = WebDriverHelper.GetById(WebDriver, "WIN_0_80077");
                ((RemoteWebDriver)WebDriver).ExecuteScript("javascript:CallARGCHG_58CRQ_58AppListEntryPoint_95Searchonbmc_45sEPFunc(false);");

                //var searchDiv = WebDriverHelper.GetById(WebDriver, "WIN_3_303683700");

                var searchText = WebDriverHelper.GetById(WebDriver, "arid_WIN_6_1000000182");
                searchText.SendKeys("CRQ000000050001");

                var searchButton = WebDriverHelper.GetById(WebDriver, "WIN_6_1002");
                searchButton.Click();

                Control.WaitEx(plugin, DefaultTimeoutSecs, true, true, 0);
            }
            catch (Exception ex)
            {
                ExecutionResult.ErrorMessage = ex.Message;
                if (ex.InnerException != null)
                {
                    ExecutionResult.ErrorMessage = ex.Message + " -----> " + ex.InnerException.Message;
                }
                ExecutionResult.ResultStatus = JobResultStatus.Fail;
            }
        }
    }
}
